const BaseController = require('../BaseController');

/**
 * 
 */
class ApiCommonController extends BaseController {
	// 接口查询
	async getApi() {
        const { ctx, app, service } = this;
        const id = ctx.params.id;
        const { type } = ctx.query;
        // body上挂在了当前要插入的表名称
        const { tableName } = ctx.request.body;
		const res = await service.admin.apiCommon.queryApi({ id, type, tableName });
		return this.ok(res);
	}
	// 创建接口
	async putApi() {
		const { ctx, app, service } = this;
		const res = await service.admin.apiCommon.createApi(ctx.request.body);
		return this.ok(res);
	}
	// 更新接口
	async postApi() {
		const { ctx, app, service } = this;
		ctx.validate({
			_id: { type: 'string', required: true }
		});
		const res = await service.admin.apiCommon.updateApi(ctx.request.body);
		return this.ok(res);
	}
	// 删除接口
	async deleteApi() {
		const { ctx, app, service } = this;
		// 解析参数主要是查询参数
		const id = ctx.params.id;
		ctx.validate({
			id: { type: 'string', required: true }
		}, { id });
		const res = await service.admin.apiCommon.deleteApi(id);
		return this.ok(res);
	}
}

module.exports = ApiCommonController;
